package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Activity;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.mapper.ActivityMapper;
import com.example.campus_activity_backend.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【t_manager_activity】的数据库操作Service实现
* @createDate 2023-11-17 14:27:21
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;
    @Override
    public long addActivity(Activity activity) {
        //参数校验
        if(activity == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"参数为空");
        }
        String activityName = activity.getName();
        String category = activity.getCategory();
        String creator = activity.getCreator();
        if(activityName == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"活动名为空");
        }else if(category == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"活动类型为空");
        }else if(creator == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"活动创建者为空");
        }
        activity.setCreatedTime(new Date());
        boolean saveResult = this.save(activity);
        if(!saveResult){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"添加失败");
        }
        return activity.getId();
    }

    @Override
    public boolean postActivity(int activityId) {
        if(activityId < 0){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR);
        }
        Activity activity = this.getById(activityId);
        if(activity == null){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR,"活动不存在");
        }
        boolean result = this.update().eq("id", activityId).setSql("status = 1").update();
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"活动发布失败");
        }
        return result;
    }

    @Override
    public List<Activity> getActivityByName(String activityName, Integer status) {
        if(activityName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"活动名称为空");
        }
        if(status == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"活动状态为空");
        }
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("name",activityName)
                .eq("status",status)
                .orderByDesc("created_time");
        List<Activity> list = this.list(queryWrapper);
        return list;
    }
}




